package com.Crawlzongheng;

import java.util.List;
import java.util.concurrent.TimeUnit;

import com.Model.NovelChapterModel;
import com.Model.NovelInfoModel;
import com.zonghengdb.ZongHengDB;

public class IntroPageThread extends Thread{
	private boolean flag = false;
	
	public IntroPageThread(String name) {
		super(name);
		
	}
	
	@Override
	public void run() {
		flag = true;
		try {
			ZongHengDB db = new ZongHengDB();
			while(flag){
				String url = db .getRankIntroPageUrl(1);
				if(null != url){
					IntroPage introPage = new IntroPage(url);
					NovelInfoModel bean = introPage.analyzer();
					if(null != bean){
						ChapterPage chapterPage = new ChapterPage(bean.getChapterListUrl());
						List<NovelChapterModel> chapters = chapterPage.analyzer();
						bean.setChapterCount(chapters == null?0:chapters.size());
						db.updateNovelInfo(bean);
						db.saveNovelChapter(chapters, bean.getId());
						
					}
					TimeUnit.MILLISECONDS.sleep(500);
				}else {
					TimeUnit.MILLISECONDS.sleep(1000);

				}
			}
			
			
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		super.run();
	}
	
	public static void main(String[] args) {
		IntroPageThread thread = new IntroPageThread("IntroPageThread");
		thread.start();
	}
}
